{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算ID3信息增益"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据集的总信息熵\n",
    "root = -(9/14)*np.log2(9/14)-(5/14)*np.log2(5/14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.24674981977443933"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按age切分\n",
    "# 第一部分age<=30:\n",
    "I1 = -(2/5)*np.log2(2/5)-(3/5)*np.log2(3/5)\n",
    "I1\n",
    "\n",
    "# 第二部分age31-40:\n",
    "I2 = -(4/4)*np.log2(4/4)\n",
    "I2\n",
    "\n",
    "# 第三部分age>40:\n",
    "I3 = -(2/5)*np.log2(2/5)-(3/5)*np.log2(3/5)\n",
    "I3\n",
    "\n",
    "#计算子节点的加权平均\n",
    "I_age = (5/14)*I1 + (4/14)*I2 + (5/14)*I3\n",
    "I_age\n",
    "\n",
    "# 根据age切分的信息增益：\n",
    "gain_age = root - I_age\n",
    "gain_age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.02922256565895487"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据income进行切分\n",
    "I_high = -(2/4)*np.log2(2/4)-(2/4)*np.log2(2/4)\n",
    "I_low = -(1/4)*np.log2(1/4)-(3/4)*np.log2(3/4)\n",
    "I_medium = -(2/6)*np.log2(2/6)-(4/6)*np.log2(4/6)\n",
    "\n",
    "I_income = (4/14)*I_high + (4/14)*I_low + (6/14)*I_medium\n",
    "\n",
    "gain_income = root-I_income\n",
    "gain_income"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.15183550136234159"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据student进行切分\n",
    "I_sn = -(4/7)*np.log2(4/7)-(3/7)*np.log2(3/7)\n",
    "I_sy = -(1/7)*np.log2(1/7)-(6/7)*np.log2(6/7)\n",
    "\n",
    "I_stu = (7/14)*I_sn+(7/14)*I_sy\n",
    "\n",
    "gain_stu = root-I_stu\n",
    "gain_stu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.04812703040826949"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据credit进行切分\n",
    "I_f = -(2/8)*np.log2(2/8)-(6/8)*np.log2(6/8)\n",
    "I_e = -(3/6)*np.log2(3/6)-(3/6)*np.log2(3/6)\n",
    "\n",
    "I_c = (8/14)*I_f + (6/14)*I_e\n",
    "\n",
    "gain_c = root-I_c\n",
    "gain_c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算C4.5信息增益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5774062828523454"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IV_age = -(4/14)*np.log2(4/14) -(5/14)*np.log2(5/14)-(5/14)*np.log2(5/14)\n",
    "IV_age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.15642756242117528"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GR_age = gain_age/IV_age\n",
    "GR_age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.018772646222418813"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IV_income =  -(4/14)*np.log2(4/14) -(6/14)*np.log2(6/14)-(4/14)*np.log2(4/14)\n",
    "\n",
    "GR_income = gain_income/IV_income\n",
    "GR_income"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.15183550136234159"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IV_stu =  -(7/14)*np.log2(7/14) -(7/14)*np.log2(7/14)\n",
    "\n",
    "GR_stu = gain_stu/IV_stu\n",
    "GR_stu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.048848615511520824"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IV_c =  -(8/14)*np.log2(8/14) -(6/14)*np.log2(6/14)\n",
    "\n",
    "GR_c = gain_c/IV_c\n",
    "GR_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
